# -*- coding: utf-8 -*-

'''
Created on 2013-9-11

@author: johnw
'''


from openerp.report import report_sxw
#overrice the setCompany method, add the context variables: company_hdr,company_adr,footer1,footer2
def setCompany(self, company_id):
    if company_id:
        self.localcontext['company'] = company_id
        self.localcontext['logo'] = company_id.logo
        self.rml_header = company_id.rml_header
        self.rml_header2 = company_id.rml_header2
        self.rml_header3 = company_id.rml_header3
        self.logo = company_id.logo
        #set header company name
        comp_obj = self.pool.get('res.company')   
        comp_cn = comp_obj.browse(self.cr,self.uid,company_id.id,context={'lang':'zh_CN'})
        comp_en = comp_obj.browse(self.cr,self.uid,company_id.id,context={'lang':'en_US'})
        comp_name_cn = comp_cn.partner_id.name
        comp_name_en = comp_en.partner_id.name
        company_adr = (comp_cn.state_id.name or '') + (comp_cn.city or '') + (comp_cn.street2 or '') + (comp_cn.street or '')
        pdf_footer1 = 'Tel: ' + (comp_en.phone or '') \
                    + ' | Fax: ' + (comp_en.fax or '') \
                    + ' | Web: ' + (comp_en.website or '') \
                    + ' | ' + (comp_en.email or '')
        pdf_footer2 = 'Add: ' + (comp_en.street or '') + ',' + (comp_en.street2 or '') + ',' + (comp_en.city or '')\
                     + ',' + (comp_en.state_id.name or '') + ',' + (comp_en.country_id.name or '') + ' ' + (comp_en.zip or '')
        self.localcontext['company_hdr'] = comp_name_cn + ' | ' + comp_name_en
        self.localcontext['company_adr'] = company_adr
        self.localcontext['footer1'] = pdf_footer1
        self.localcontext['footer2'] = pdf_footer2
        
#new methods used by sale_order and purchase_order to get the uom digits
def get_uom_digits(self,uom):
    if not hasattr(self, 'uom_digits'):
        self.uom_digits = {}
    if self.uom_digits.has_key(uom.name):
        return self.uom_digits.get(uom.name)
    digits = 0
    round = uom.rounding
    while round < 1:
        round = round*10
        digits += 1
    self.uom_digits.update({uom.name:digits})
    return digits 
    
report_sxw.rml_parse.setCompany = setCompany
report_sxw.rml_parse.get_uom_digits = get_uom_digits